/**
 * Created by karl.zheng on 2018/3/14.
 */
import md5 from './md5'
import {server, api} from './index'
import {HTTPUtil} from './http'
import {insertAccount, initOrder, showAlert, initUser} from '../actions'
import {createStore} from 'redux'
import {hashHistory} from 'react-router'
import reducer from '../reducers'

export const store = createStore(reducer);

//参数签名
export const signed = (params) => {
    var str = "";
    for(var key in params){
        str+=params[key];
    }

    // console.log(str+server.appKey);
    return md5(str+server.appKey);
}

//设备信息
export const deviceMsg = () => {
    var arr = ["source", "advChannel", "network", "model", "operatorOs", "deviceNo", "device", "version", "sdkVersion"];
    var params = {};
    arr.forEach(key =>
        params[key] = "0"
    );

    var browser = {
        versions: function () {
            var u = navigator.userAgent;
            return {
                ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
                iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,
                iPad: u.indexOf('iPad') > -1,
                android: u.indexOf('Android') > -1
            };
        }(),
    };

    if(browser.versions.ios || browser.versions.iPhone || browser.versions.iPad){
        params.source = 0;
        params.deviceNo = "IDFV";
        params.device = "IDFV";
    } else if(browser.versions.android){
        params.source = 1;
        params.deviceNo = "IMEI";
        params.device = "MAC#ANDRIDID";
    } else {
        params.source = 3;
    }
    params.network = 0;
    params.appId = server.appId;
    params.advChannel = server.advChannel;

    return params;
}

//fb sdk 初始化
export const fbInit = () => {
    (function(d, s, id) {
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) return;
        js = d.createElement(s); js.id = id;
        js.src = "https://connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

    window.fbAsyncInit = function() {
        FB.init({
            appId      : '122460745081572',
            // appId      : '1170290033098881',
            cookie     : true,  // enable cookies to allow the server to access
                                // the session
            xfbml      : true,  // parse social plugins on this page
            version    : 'v2.8' // use graph api version 2.8
        });

        FB.getLoginStatus(function(response) {
            statusChangeCallback(response);
        });
    };

    var head = document.getElementsByTagName("head")[0];
    var str = [
        {
            property: "og:url",
            content: "http://sdk-test.changic.net.cn:8191/sdk-demo/demo.html"
        },
        {
            property: "og:type",
            content: "website"
        },
        {
            property: "og:title",
            content: "Neue legendäre Pokemons kommen im Sicht!"
        },
        {
            property: "og:description",
            content: "Begrüßte die neue Version des klassischen Abenteuerspieles &lt;Trainer Go&gt;, wird dieses Update neue Pokemons, neue Art und Weise und Features bringen als auch die Champions Liga, auf die die Trainer lange gewartet haben. Nehmt Ihr die Veranstaltung teil um viele attraktive Geschenke zu bekommen: Diamant, normales Fragment, Blitz-Münze, Pokemons-Fragment....."
        },
        {
            property: "og:image",
            content: "http://sdk-test.changic.net.cn:8191/sdk-demo/img/fb-share.png"
        }
    ]

    str.map(node => {
        var meta = document.createElement('meta');
        meta.setAttribute("property", node.property);
        meta.setAttribute("content", node.content);
        document.getElementsByTagName('head')[0].appendChild(meta);
    });
}

//登录
export const reqLogin = (username, password) => {
    var data = {
        appId: server.appId,
        userName: username,
        password: password,
    };
    data = Object.assign(deviceMsg(), data);
    data.sign = signed({
        appId: server.appId,
        userName: username,
        password: password,
        source: data.source
    });

    var url = api.ip + api.login;
    LoginRequest(url, data)
};

//注册 & fb 登录
export const reqRegister = (params) => {
    params.password = md5(params.password);
    var data = Object.assign(deviceMsg(), params);
    data.sign = signed({
        appId: server.appId,
        userName: params.userName,
        password: params.password,
        source: data.source
    })
    var url = api.ip + api.register;
    LoginRequest(url, data)
}

//fb sdk 调起登录插件
export const fbLogin = () => {
    // var response = '{"authResponse":{"accessToken":"EAAQDWTTZAVEIBAALSnOLqpehbtPDOZC0IiaA4cScu5IRRKNtaLOZB2J2G8jZA9WZBVUj0nNGAbDYhGHZAZADOiaG60vZCV4MJbJTIeZCqbR7SVFnAIaeUZAZC6YV1wfdwiG1d7fbzJLVoKXKGs9wvqnFsPKZC40LCYJlM7RukNoKXyU5fPS11N27hCG2OcjWfiN4dD97SP3ZAxgQfigZDZD","userID":"353436268389818","expiresIn":4152,"signedRequest":"Rn0oZXjNOXGGKTKdG54bUAtz_9W6CNKnvrHpKqc-4YA.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImNvZGUiOiJBUUM1Z3hPLTVUTkpMSVNCSFhGOEN2dGNxaWJ6UXhhVVZoM1MyX3lGRGtRQURPVzFjTHFrZVBndXVMSkVhVHNleVVZX1NHTEtIb3FwTHpaa1VhV2RZNHZRdkJCNC1KOC14LUdFcDVST3E5REFFaWFaREhONDVTMVNaNmN5ZXFjZ0VvZ1ZFUjhJREFpbEpqdEFXZmM1WnRjVnp4dm16Q2lneU5nQ3d5OFVhRXlpdHBuT2lRaW9rZ3V2UnZNRkg2X2lvcXlJaXY1QmRCMkVEVFF2eUJrR3NuS1c0ajZIVmlPeUxXcWEtTENwazBEaEU4OEYyVXNMdnhZOHh5aHFFYm1kYU1jWmQ0MEY3NEZVaEJsenFKRlhnNUJoLWMtenhxbDFLRHBYTXp6MGJoanVYek43d0FBTGVaR1NIYkpoOWlERWpQTkVrMERSYnJmR0lTMlZLSG1ZUTVzaSIsImlzc3VlZF9hdCI6MTUyMTcyMzA0OCwidXNlcl9pZCI6IjM1MzQzNjI2ODM4OTgxOCJ9"},"status":"connected"}';
    // response = JSON.parse(response);
    FB.login(function(response){
        console.log(JSON.stringify(response));
        if (response.status === 'connected') {
            var userId = response.authResponse.userID;
            var params = {
                userName: 'fb-'+userId,
                password: "",
                appId: server.appId,
                accountType: 2,
                thirdPartyId: "",
                email: "",
                telephone: '',
                userChannel: 0,
                exInfo: JSON.stringify([{"scopeId":"1459051407752747","fbId": server.fbId}])
            };
            reqRegister(params);
        } else {
            alert("fb login fail");
        }
    });
};

export const getUserName = (accessToken) => {
    var url = "https://graph.facebook.com/me?access_token="+accessToken;
    fetch(url, {
        method: 'GET',
        headers: {
            'Accept': 'application/json, text/javascript, */*; q=0.01',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
        },
        mode: 'cors',
        credentials: 'credentials',
        cache: 'default'
    }).then((resp) => {
        try {
            return resp.json();
        } catch (err) {}
        return resp.text();
    }).then((data) => {
        console.log(data);
    }).catch((err) => {
        console.log(err);
    });
};

/**
 * 登录部分请求函数
 * url 请求链接
 * params 请求参数
 * */
export const LoginRequest = (url, params) => {
    HTTPUtil(url, params).then((res) => {
        // res = '{"data":{"emailValid":1,"email":"Qiong@9266.com","userId":25086659,"accountType":1,"firstLogin":0,"userName":"Qiong@9266.com","userType":1},"token":"e802994c26bc4308aa343dd66b09b7d9","firstLogin":false,"code":200,"error_msg":""}';
        // res = '{"data":{"emailValid":0,"userId":1000001025,"accountType":0,"firstLogin":1,"userName":"9815813738","userType":0},"token":"5eddb542397648619b7a2f0669a568cc","firstLogin":false,"code":200,"error_msg":""}';
        // res = JSON.parse(res);
        // console.log(res);
        if(res.code == 200){
            var data = {
                accountType: res.data.accountType,
                userId: res.data.userId,
                userName: res.data.userName,
                userType: res.data.userType,
                password: params.password,
                token: res.token,
                activeTime: new Date().getTime()
            };
            store.dispatch(insertAccount(data));
            hashHistory.push('/login/loading');
            return true;
        }else{
            alert("请求失败");
            return false;
        }
    } )
};

//绑定区服
export const bindZone = (params) => {
    var url = api.ip + api.bindZone;
    var data = Object.assign(params, deviceMsg());
    var signParams = {
        userId: params.userId,
        appId: server.appId,
        gameZoneId: params.gameZoneId,
        source: params.source
    };

    data.sign = signed(signParams);
    HTTPUtil(url, data).then((res) => {
        console.log(res);
    });
}


//自动登录
export const AutoLogin = (account) => {
    switch (account.accountType){
        case 0:
            reqLogin(account.userName, account.password);
            break;
        case 1:
            reqLogin(account.userName, account.password);
            break;
        case 2:
            var params = {
                userName: account.userName,
                password: "",
                appId: server.appId,
                accountType: 2,
                thirdPartyId: "",
                email: "",
                telephone: '',
                userChannel: 0,
                exInfo: JSON.stringify([{"scopeId":"1459051407752747","fbId": server.fbId}])
            };
            reqRegister(params);
            break;
        default:
            break;
    }
}

/***
 * 获取支付方式
 * */
export const getPay = () => {
    var params = store.getState().order;
    params.appId= server.appId;
    var data = Object.assign(deviceMsg(), params);
    data.sign = signed({
        appId: server.appId,
        advChannel: data.advChannel,
        userId: data.userId,
        gameCoin: data.gameCoin,
        level: data.level,
        source: data.source,
        network: data.network
    });

    var url = api.ip + api.getPay;
    return HTTPUtil(url, data);
}

/**
 * 游戏那边获取订单id
 * 下单
 * */
export const loadPay = (params) => {
    var order = store.getState().order;
    var time = new Date();
    var params = {
        appId: server.appId,
        channel: params.channel,
        code: params.code,
        amount: params.selectedProduct.amount,
        currency: params.selectedProduct.currency,
        productName: params.selectedProduct.productName,
        itemType: params.selectedProduct.itemType,
        isOfficial: params.isOfficial,
        clientTime: time.format("yyyy-MM-dd hh:mm:ss"),
        exInfo: params.exinfo?params.exinfo:"0"
    };

    var data = Object.assign(order, deviceMsg(), params);

    data.sign = signed({
        appId: server.appId,
        advChannel: data.advChannel,
        userId: data.userId,
        roleId: data.roleId,
        gameOrderId: data.gameOrderId,
        gameZoneId: data.gameZoneId,
        code: data.code,
        source: data.source,
        channel: data.channel,
        amount: data.amount,
        currency: data.currency,
        productName: data.productName,
        exInfo: data.exInfo
    })

    var url = api.ip + api.loadPay;
    return HTTPUtil(url, data);
}

Date.prototype.format = function (fmt) { //author: meizz
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

export const login = () => {
};

export const login2 = () => {
    hashHistory.push('/login');
}

export const pay = (params) => {
    store.dispatch(initOrder(params));
    var html = document.documentElement;
    if(html.clientWidth < 750 && html.clientHeight > 375){
        hashHistory.push('/payment');
    }else{
        hashHistory.push('/pay');
    }
}

export const Logout = () => {
    // store.dispatch(logout());
    store.dispatch(showAlert());
}

//屏幕适应
export const winResize = function(){
    var evt = "onorientationchange" in window ? "orientationchange" : "resize";
    window.addEventListener(evt, resizefn, false);//添加屏幕旋转事件
};

export const resizefn = function() {
    var html = document.documentElement;
    var k = 750; //设计稿宽度
    if(html.clientWidth < 750){
        html.style.fontSize = html.clientWidth / k * 100 + "px";
    }else{
        html.style.fontSize = "100px";
    }
};

//更改密码
export const changePass = (newpass) => {
    var data = {
        userId: store.getState().user.userId,
        appId: server.appId,
        userName: store.getState().user.userName,
        newPassword: newpass
    };
    data.sign = signed({
        userId: data.userId,
        userName: data.userName,
        newPassword: newpass
    });

    var url = api.ip + api.changePass;
    return HTTPUtil(url, data);
}

export const share = () => {
    /*分享 start*/
    FB.ui({
        method: 'feed',
        link: 'http://sdk-test.changic.net.cn:8191/sdk-demo/demo.html',
    }, function(response){
        if(response){
            console.log("分享完成");
        }
    });
}

export const messager = () => {
    window.open("https://m.me/zhengyl36");
}

export const fb = () => {
    var useragent = navigator.userAgent; // cache the userAgent info
    var iPhone = (useragent.match(/(iPad|iPhone|iPod)/g));
    var scheme;
    if (iPhone)
        scheme = "fb://page/?id=1738994216110921";
    else
        scheme = "fb://page/1738994216110921";

    uriSchemeWithHyperlinkFallback(scheme, "https://www.facebook.com/LunaMobile.Asia/");
}

function uriSchemeWithHyperlinkFallback(uri, href) {
    if (!window.open(uri)) {
        window.location = href;
    }
}

export const getUserInfo = () => {
    if(store.getState().user)
        return store.getState().user;
    return {}
}

export const point = (name) => {
    switch(name){
        case "create-role":
            gtag('event', 'create_role', {
                'event_category': 'engagement',
                'event_label': 'create_role'
            });
            fbq('track', 'CompleteRegistration');
            break;
        case "task-4":
            gtag('event', 'task', {
                'event_category': 'engagement',
                'event_label': 'task_4'
            });
            fbq('track', 'Search');
            break;
        case "auto-task":
            gtag('event', 'task', {
                'event_category': 'engagement',
                'event_label': 'Martial'
            });
            fbq('track', 'Lead');
            break;
        case "loginreward-2":
            gtag('event', 'get', {
                'event_category': 'gift',
                'event_label': 'loginreward_2'
            });
            fbq('track', 'AddToWishlist');
            break;
        case "first-purchase":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'first purchase'
            });
            fbq('track', 'Purchase');
            break;
        case "purchase-10k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_10k'
            });
            fbq('track', 'AddPaymentInfo');
            break;
        case "purchase-20k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_20k'
            });
            fbq('track', 'AddToCart');
            break;
        case "purchase-30k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_30k',
                'value': 1.32
            });
            break;
        case "purchase-50k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_50k'
            });
            break;
        case "purchase-100k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_100k'
            });
            fbq('track', 'InitiateCheckout');
            break;
        case "purchase-200k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_200k'
            });
            break;
        case "purchase-300k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_300k'
            });
            break;
        case "purchase-500k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_500k'
            });
            break;
        case "purchase-1000k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_1000k'
            });
            fbq('track', 'ViewContent');
            break;
        case "purchase-2000k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_2000k'
            });
            break;
        case "purchase-5000k":
            gtag('event', 'purchase', {
                'event_category': 'ecommerce',
                'event_label': 'original_5000k'
            });
            break;
        default:
            break;
    }
};

window.SDK = {
    Login: login,
    Login2: login2,
    Pay: pay,
    BindZone: bindZone,
    Logout: Logout,
    FbInit: fbInit,
    Share: share,
    Messager: messager,
    Fb: fb,
    GetUserInfo: getUserInfo,
    Point: point
}

export const setHeader = ()=>{
    let str = [
        `<script async src="https://www.googletagmanager.com/gtag/js?id=UA-117997360-1"></script>
        <script>
          window.dataLayer = window.dataLayer || [];
          function gtag(){dataLayer.push(arguments);}
          window.gtag = gtag;
          gtag('js', new Date());
        
          gtag('config', 'UA-117997360-1');
        </script>
        <script> 
            !function(f,b,e,v,n,t,s)
            {if(f.fbq)return;n=f.fbq=function(){n.callMethod?
                n.callMethod.apply(n,arguments):n.queue.push(arguments)};
                if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
                n.queue=[];t=b.createElement(e);t.async=!0;
                t.src=v;s=b.getElementsByTagName(e)[0];
                s.parentNode.insertBefore(t,s)}(window, document,'script',
            'https://connect.facebook.net/en_US/fbevents.js');
            fbq('init', '1490773921035020');
            fbq('track', 'PageView');
        </script>
        <noscript>
            <img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1490773921035020&ev=PageView&noscript=1"/>
        </noscript>`
    ];

    var header = document.getElementsByTagName('head')[0];
        header.innerHTML+=str;
};

setHeader();
